package com.qianli.cashmama.trade.repo.impl;

import com.qianli.cashmama.trade.common.enums.BizTypeEnum;
import com.qianli.cashmama.trade.dao.TBizRecordDao;
import com.qianli.cashmama.trade.domain.model.BizRecord;
import com.qianli.cashmama.trade.entity.TBizRecordEntity;
import com.qianli.cashmama.trade.repo.BizRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2018/1/25
 * Time: 下午5:29
 */
@Repository
public class BizRecordRepositoryImpl implements BizRecordRepository {

    @Autowired
    private TBizRecordDao bizRecordDao;

    @Override
    public String store(BizRecord bizRecord) {
        TBizRecordEntity bizRecordEntity = new TBizRecordEntity()
                .setBizNo(bizRecord.getBizNo())
                .setInnerNo(bizRecord.getInnerNo())
                .setBizType(bizRecord.getBizType().getBizType());
        //保持打款唯一性
        bizRecordDao.deleteTBizRecordByInnerNo(bizRecord.getInnerNo(), bizRecord.getBizType().getBizType());
        bizRecordDao.insert(bizRecordEntity);
        return bizRecord.getBizNo();
    }

    @Override
    public BizRecord load(String bizNo) {
        TBizRecordEntity bizRecordEntity = bizRecordDao.selectOneByBizNo(bizNo);
        if (bizRecordEntity == null) {
            return null;
        }

        return new BizRecord()
                .setBizNo(bizNo)
                .setBizType(BizTypeEnum.getEnumByType(bizRecordEntity.getBizType()))
                .setInnerNo(bizRecordEntity.getInnerNo());
    }
}
